package top.jacktgq.itheima.netty.protocol_design.groupchat.message;

import io.netty.channel.ChannelHandlerContext;
import lombok.Data;
import lombok.ToString;
import top.jacktgq.itheima.netty.protocol_design.groupchat.server.session.GroupSessionFactory;

import java.util.Set;

@Data
@ToString(callSuper = true)
public class GroupMembersRequestMessage extends Message {
    private String groupName;

    public GroupMembersRequestMessage() {
    }

    public GroupMembersRequestMessage(String groupName) {
        this.groupName = groupName;
    }

    @Override
    public int getMessageType() {
        return GroupMembersRequestMessage;
    }

    @Override
    public void handle(ChannelHandlerContext ctx) {
        Set<String> members = GroupSessionFactory.getGroupSession().getMembers(groupName);
        ctx.writeAndFlush(new GroupMembersResponseMessage(groupName, members));
    }
}
